package cn.iocoder.yudao.module.system.dal.redis.mail;


import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.Duration;

import static cn.iocoder.yudao.module.system.dal.redis.RedisKeyConstants.MAIL_CAPTCHA_CODE;

/**
 * 统计计数redis
 */
@Repository
public class MailRedisDAO {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public String get(String id) {
        String redisKey = formatKey(id);
        return stringRedisTemplate.opsForValue().get(redisKey);
    }
    public Boolean hast(String id) {
        String redisKey = formatKey(id);
        return (Boolean.TRUE.equals(stringRedisTemplate.hasKey(redisKey)));
    }
    public void set(String id, String code, Duration timeout) {
        String redisKey = formatKey(id);
        stringRedisTemplate.opsForValue().set(redisKey, code,timeout);
    }

    public void delete(String id) {
        String redisKey = formatKey(id);
        stringRedisTemplate.delete(redisKey);
    }
    private static String formatKey(String key) {
        return String.format(MAIL_CAPTCHA_CODE.getKeyTemplate(), key);
    }

}
